package com.rftennis.service.impl;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.security.auth.login.LoginException;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.rftennis.ao.CustomerAo;
import com.rftennis.dao.CustomerDao;
import com.rftennis.dao.SqlSessionUtil;

import com.rftennis.dto.CustomerContext;

import com.rftennis.entity.CustomerEntity;
import com.rftennis.service.CustomerService;

@Service
public class CustomerServiceImpl implements CustomerService {
	@Autowired
	CustomerDao cDao;
	
	//实现注册方法
	public CustomerContext register(CustomerAo ao) throws Exception {
		
		//1验证参数
		//1.1 都不能为空(null、空字符串、空白字符串)
		String   name=assertNotBlank("name不能为空", ao.getName());
		String   account=assertNotBlank("account不能为空", ao.getAccount());
		String	password=assertNotBlank("password不能为空", ao.getPassword());
		String	phoneNumber=assertNotBlank("phoneNumber不能为空", ao.getPhoneNumber());
		String	mail=assertNotBlank("mail不能为空", ao.getEmail());
		String	address=assertNotBlank("address不能为空", ao.getAddress());
				//1.2 长度约束
				assertLessThan("name非法", password, 16);
				assertLessThan("account非法", ao.getAccount(), 32);
				assertLessThan("password非法", ao.getPassword(), 16);
				assertLessThan("phoneNumber非法", ao.getPhoneNumber(), 16);
				assertLessThan("mail非法", ao.getEmail(), 16);
				assertLessThan("address非法", ao.getAddress(), 32);
				
				//1.3 其他约束(省略)
		//2执行逻辑
				if(cDao.select("account",account)!=null) {
					 throw new Exception("账号已存在,注册失败");
				}
				CustomerEntity cEntity=new CustomerEntity();
				cEntity.setId(uuid());
				cEntity.setName(name);
				cEntity.setAccount(account);
				cEntity.setPassword(password);
				cEntity.setPhoneNumber(phoneNumber);
				cEntity.setMail(mail);
				cEntity.setAddress(address);
				cEntity.setCreateTime(LocalDateTime.now());
				cDao.insert(cEntity);
		//3组装结果
				CustomerContext c=new CustomerContext();
				c.setId(cEntity.getId());
				c.setAccount(cEntity.getAccount());
				c.setName(cEntity.getName());
				c.setPhoneNumber(cEntity.getPhoneNumber());
				c.setMail(cEntity.getMail());
				c.setAddress(cEntity.getAddress());
				c.setCreateTime(cEntity.getCreateTime());
				return c;
				
			
		}
	
 

	private String uuid() {
		String uuid = UUID.randomUUID().toString();
		char[] cs = new char[32];
		char c;
		for (int i = uuid.length(), j = 0; i-- > 0;) {
			if ((c = uuid.charAt(i)) != '-') {
				cs[j++] = c;
			}
		}
		return new String(cs);
	}



	//实现登陆方法
	public CustomerContext login(String account, String password) throws Exception {
		//1验证参数
		//1.1 都不能为空(null、空字符串、空白字符串)
		account=assertNotBlank("account不能为空", account);
		password=assertNotBlank("password不能为空", password);
		
		//1.2 长度约束
		assertLessThan("account非法", account, 32);
		assertLessThan("password非法", password, 16);
		//1.3 其他约束(省略)
//2执行逻辑
		CustomerEntity cEntity=cDao.select("account",account);
		if(cEntity==null) {
			throw new LoginException("account-and-password.not-match");
		}
		//2.2 判断密码是否正确
		if(!password.equals(cEntity.getPassword())) {//注意顺序
			throw new LoginException("account-and-password.not-match");
		}
		//2.3 更新最后登录时间
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("lastLoginTime",LocalDateTime.now());
		cDao.update(cEntity.getId(),map);
		//3 组装结果
		CustomerContext c=new CustomerContext();
		c.setId(cEntity.getId());
		c.setAccount(cEntity.getAccount());
		c.setName(cEntity.getName());
		c.setPhoneNumber(cEntity.getPhoneNumber());
		c.setMail(cEntity.getMail());
		c.setAddress(cEntity.getAddress());
		c.setCreateTime(cEntity.getCreateTime());
		c.setLastLoginTime(LocalDateTime.now());
		return c;

	}
	
	//实现修改密码方法
	public void updatePassword(String id, String oldPassword, String newPasswrod, String newPasswordConfirm)throws Exception {
		//1验证参数
		//2执行逻辑
	   //3组装结果
		
	}
	
}
